package cn.tx.api.controller;

import cn.tx.hotel.service.IHotelService;
import cn.tx.pojo.domain.Hotel;
import cn.tx.constants.HotelMqConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.InvalidParameterException;
import java.util.Map;

/**
 * 发送MQ消息测试
 */
@RestController
@RequestMapping("api/hotel/")
public class HotelController {

    private static final Logger logger = LoggerFactory.getLogger(HotelController.class);

    @Autowired
    private IHotelService hotelService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送MQ
     * @param params
     */
    @GetMapping("query")
    public void query(@RequestParam Map<String, Object> params){
        logger.info("ApiApplication-[api/hotel/query]");
        // 查询酒店
        hotelService.queryList(params);

        // 发送MQ消息
        rabbitTemplate.convertAndSend(HotelMqConstants.HOTEL_EXCHANGE_NAME, HotelMqConstants.HOTEL_QUERY_KEY, params);
    }


    /**
     * 保存方法
     * @param hotel
     */
    @PostMapping("saveHotel")
    public void saveHotel(@RequestBody Hotel hotel){
        logger.info("ApiApplication-[api/hotel/saveHotel]");
        // 新增酒店
        hotelService.save(hotel);

        // 发送MQ消息
        rabbitTemplate.convertAndSend(HotelMqConstants.HOTEL_EXCHANGE_NAME, HotelMqConstants.HOTEL_INSERT_KEY, hotel.getId());
    }

    /**
     * 更新
     * @param hotel
     */
    @PutMapping("updateById")
    public void updateById(@RequestBody Hotel hotel){
        if (hotel.getId() == null) {
            throw new InvalidParameterException("id不能为空");
        }
        logger.info("ApiApplication-[api/hotel/updateById]");
        hotelService.updateById(hotel);

        // 发送MQ消息
        rabbitTemplate.convertAndSend(HotelMqConstants.HOTEL_EXCHANGE_NAME, HotelMqConstants.HOTEL_INSERT_KEY, hotel.getId());
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        logger.info("ApiApplication-[api/hotel/deleteById]");
        hotelService.removeById(id);

        // 发送MQ消息
        rabbitTemplate.convertAndSend(HotelMqConstants.HOTEL_EXCHANGE_NAME, HotelMqConstants.HOTEL_DELETE_KEY, id);
    }
}
